package com.health.db.dao.user.impl;
 
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IStuffListDao;
import com.health.db.dto.DoctorListDto;
import com.health.db.dto.StuffDetailDto;
import com.health.db.mapper.StuffListMapper;
import com.health.db.model.PatientListTemp;
import com.health.db.model.StuffList;
import com.health.db.model.StuffListExample;
import com.health.redis.RedisConstants;

/**
 * Created by helili on 2017/2/23.
 */
public class StuffListDaoImpl extends ServiceInterface implements IStuffListDao {
	
	StuffListMapper stuffListMapper;
	 
	//查询方法名列表  redis
	private String selectByDepartmentId=":departmentId:";
	private String isUserRegisterVisible=":isUserRegisterVisible:";
	private String  isUsedFlag=":isUsedFlag:";
	private String selectByStuffId=":StuffId:";
	private String selectByStuffNo =":StuffNo:";
	private String offset=":OF:";
	private String pageSize=":PS:";
	private String registerWhere=":registerWhere:";
	private String solt = ":solt:";
	private String weekDay =":weekDay:";
	
	private String assistant =":assistant:";
	
    
	public StuffListMapper getStuffListMapper() {
		return stuffListMapper;
	}
	
	public void setStuffListMapper(StuffListMapper stuffListMapper) {
		this.stuffListMapper = stuffListMapper;
	}
	/**
	 * req object
	 * {
	 * hospitalId 
	 * departmentid
	 * isUserRegisterVisible 是否可见  1可见 2不可见
	 * isUsedFlag  是否再用  1未启用 0启用
	 * registerWhere 咨询类型
	 * solt    时间段
	 * weekDay 星期
	 * offset  偏移量
	 * pageSize  查询数量
	 * }
	 * 
	 */
	@Override
    public List<DoctorListDto> selectByQueryDocReq(StuffListExample req) { 
        
    	getRedisOperatiorLogic();
     	String key=RedisConstants.StuffListMapper+selectByDepartmentId+req.getDepartmentid()
     			+isUserRegisterVisible+req.getIsUserRegisterVisible()
     			+isUsedFlag+req.getIsUsedFlag();
     	
     	if (StringUtils.isNotBlank(req.getRegisterWhere())) {
     		key=key+registerWhere+req.getRegisterWhere();
		}
     	if (StringUtils.isNotBlank(req.getSolt())) {
     		key=key+solt+req.getSolt()+weekDay+req.getWeekday();
		}
     	key = key+offset+req.getOffset()+pageSize+req.getPageSize();
     	Object object=null;
     	List<DoctorListDto> doctorListDtoList=null;
        		
 		 if(redisOperatiorLogic.isJedisIsConnected())
 		 {
 			 object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
 			 
 		 }
 		 if(null!=object)
 		 {
 			 if ( !"".equals(object)) {
 				doctorListDtoList=(List<DoctorListDto>) object;
			}
 		 }
 		 else
 		 {
 			doctorListDtoList = stuffListMapper.selectByQueryDocReq(req);
 			if(redisOperatiorLogic.isJedisIsConnected())
    		{
 				redisOperatiorLogic.setEx(key, doctorListDtoList, getOrderRedisGroupNo("1"));
    		}
 		 }     
        return doctorListDtoList;
    }

	/**
	 * stuffId 医生id
	 */
    @Override
    public StuffDetailDto selectByStuffId(Long stuffId) {
 
    	getRedisOperatiorLogic();
     	String key=RedisConstants.StuffListMapper+selectByStuffId+stuffId;
     	Object object=null;
     	StuffDetailDto stuffDetailDto=null; 
     
		 if(redisOperatiorLogic.isJedisIsConnected())
		 {
			 object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		 }
		 if(null!=object)
		 {
			 if ( !"".equals(object)) {
					stuffDetailDto=(StuffDetailDto) object;
			}
		 }
		 else
		 {
			 stuffDetailDto = stuffListMapper.selectByStuffId(stuffId);
			if(redisOperatiorLogic.isJedisIsConnected())
			 {
			 redisOperatiorLogic.setEx(key, stuffDetailDto, getOrderRedisGroupNo("1"));
			 }
		 }
        return  stuffDetailDto;
    }


    /**
     * stuffNo 医生电话号码
     */
	@Override
	public StuffDetailDto selectByStuffNo(String stuffNo) {
		
		getRedisOperatiorLogic();
     	String key=RedisConstants.StuffListMapper+selectByStuffNo+stuffNo;
     	Object object=null;
     	StuffDetailDto stuffDetailDto=null; 
 		 if(redisOperatiorLogic.isJedisIsConnected())
 		 {
 			 object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
 		 }
 		 if(null!=object)
 		 {
 			 if ( !"".equals(object)) {
 				 stuffDetailDto=(StuffDetailDto) object;
 			 }
 		 }
 		 else
 		 {
 			 stuffDetailDto = stuffListMapper.selectByStuffNo(stuffNo);
 			if(redisOperatiorLogic.isJedisIsConnected())
    		 {
 			 redisOperatiorLogic.setEx(key, stuffDetailDto, getOrderRedisGroupNo("1"));
    		 }
 		}
        return  stuffDetailDto;
	}
    
	
	public static void main(String[] args) {
		
		String ss = "131143-383380";
		
		String[] str  = ss.split("-");
		
		
		for (int i = 0; i < str.length; i++) {
			
			System.out.println(str[i]);
		}
		
	}

	@Override
	public List<StuffList> selectByExample(StuffListExample example) {
		// TODO Auto-generated method stub
		getRedisOperatiorLogic();
		String key=RedisConstants.StuffListMapper+assistant+example.getStuffAlias();
		
		Object object=null;
		List<StuffList> stuffList = null;
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{	
			object = redisOperatiorLogic.readObject(key, getPublicRedisGroupNo("1"));
		 }
	     if(null!=object){
	    	 if (!object.equals("")) {
	    		 stuffList =  (List<StuffList> ) object;
			}
	     }else{
	    	 stuffList=stuffListMapper.selectByExample(example);
             if(redisOperatiorLogic.isJedisIsConnected())
				{redisOperatiorLogic.setEx(key, stuffList,  getOrderRedisGroupNo("1"));}
		 }
		return stuffList;
	}
}
